<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

</head>
<body>
  <ul></ul>
  <script lang="javascript">
  let dataArr = [
      {
          "id": 1,
          "pid": 0,
          "name": '一级1.0.1'
      },
      {
          "id": 2,
          "pid": 0,
          "name": '一级1.0.2'
      },
      {
          "id": 3,
          "pid": 1,
          "name": '二级2.1.3'
      },
      {
          "id": 7,
          "pid": 1,
          "name": '二级2.1.7'
      },
      {
          "id": 6,
          "pid": 7,
          "name": '三级3.7.6'
      },
      {
          "id": 5,
          "pid": 7,
          "name": '三级3.7.5'
      },
  ];
  function arrayTotree(arr,rootvalue){
    let newArr = [];
    arr.forEach(item => {
      if(item.pid===rootvalue){
      //找到之后，就要看item下有没有子节点
      //条件不能是（arr，rootvalue），否则死循环
        const children = arrayTotree(arr,item.id);
        if(children.length){
        //children长度大于0，说明有子节点
          item.children = children;
        }
        //把找到的数据内容加入数组中
        newArr.push(item);
      }
    });
    return newArr;
  }
  let listArr = arrayTotree(dataArr,0) 
  // console.log(listArr)  
  let list = [];
  function getList(treeData){
    treeData.map((item,index)=>{
      let {id,name,pid} =item;
      list.push({id,name,pid});
      if(item.children!==undefined&&item.children.length>0){
        getList(item.children)
      }
    })
    return list;
  }
  console.log(getList(listArr))
  </script>
</body>
</html>